<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="author" content="esy">
    
    <meta name="description" content="esy">
    
    
    
    
    
    
    <title>python_随机森林参数设置 | ESY</title>
    <link href="https://esyyes.github.io" rel="prefetch" />

    
<link rel="stylesheet" href="/css/bootstrap.min.css">
<link rel="stylesheet" href="/css/aos.css">
<link rel="stylesheet" href="/css/style.css">

    
<script src="/js/jquery.min.js"></script>

    
<script src="/js/bootstrap.min.js"></script>

    
<script src="/js/aos.js"></script>

    
<script src="/js/highslide/highslide-full.min.js"></script>

    
<link rel="stylesheet" href="/js/highslide/highslide.css">

    <style type="text/css">
        @media (max-width: 768px) {
            body {
                background-color: #f0f0f0;
                background: url('/imgs/xsbg.gif');
                background-attachment: fixed;
            }
        }
    </style>
    
    <!--<script type="text/javascript">
      if (document.images) {
        var avatar = new Image();
        avatar.src = '/imgs/avatar.jpg'
        var previews = 'preview1.jpg,preview2.jpg,preview3.jpg,preview4.jpg'.split(',')
        var previewsPreLoad = []
        for(var i = 0; i < length; i++) {
          previewsPreLoad.push(new Image())
          previewsPreLoad[previewsPreLoad.length - 1].src = '/imgs/preview' + previews[i]
        }
      }
    </script>-->
<meta name="generator" content="Hexo 5.2.0"></head>
<body>
    <!-- 背景轮播图功能 -->
    <section class="hidden-xs">
    <ul class="cb-slideshow">
        <li><span>天若</span></li>
        <li><span>有情</span></li>
        <li><span>天亦老</span></li>
        <li><span>我为</span></li>
        <li><span>长者</span></li>
        <li><span>续一秒</span></li>
    </ul>
</section>
    <!-- 欧尼酱功能, 谁用谁知道 -->
    
    <div class="gal-menu gal-dropdown">
    <div class="circle" id="gal">
        <div class="ring">
            <a href="https://esyyes.github.io" class="menuItem" style="left: 50%; top: 15%;">首页</a>
            
            <a class="menuItem" style="left: 80.3109%; top: 32.5%;">下一页</a>
            
            <a href="/archives" class="menuItem" style="left: 80.3109%; top: 67.5%;">归档</a>
            <a href="/about" class="menuItem" style="left: 50%; top: 85%;">关于</a>
            <a href="/message" class="menuItem" style="left: 19.6891%; top: 67.5%;">留言板</a>

            
            <a class="menuItem" style="left: 19.6891%; top: 32.5%;">上一页</a>
            
        </div>
        <audio id="audio" src="/imgs/oni.mp3"></audio>
    </div>
</div>
    
    <header class="navbar navbar-inverse" id="gal-header">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed"
                    data-toggle="collapse" data-target=".bs-navbar-collapse"
                    aria-expanded="false">
                <span class="fa fa-lg fa-reorder"></span>
            </button>
            <a href="https://esyyes.github.io">
                
                <style>
                    #gal-header .navbar-brand {
                        height: 54px;
                        line-height: 24px;
                        font-size: 28px;
                        opacity: 1;
                        background-color: rgba(0,0,0,0);
                        text-shadow: 0 0 5px #fff,0 0 10px #fff,0 0 15px #fff,0 0 20px #228DFF,0 0 35px #228DFF,0 0 40px #228DFF,0 0 50px #228DFF,0 0 75px #228DFF;
                    }
                </style>
                <!-- 这里使用文字(navbar_text or config.title) -->
                <div class="navbar-brand">ESY</div>
                
            </a>
        </div>
        <div class="collapse navbar-collapse bs-navbar-collapse">
            <ul class="nav navbar-nav" id="menu-gal">
                
                
                <li class="">
                    <a href="/">
                        <i class="fa fa-home"></i>首页
                    </a>
                </li>
                
                
                
                <li class="">
                    <a href="/archives">
                        <i class="fa fa-archive"></i>归档
                    </a>
                </li>
                
                
                
                
                <li class="dropdown">
                    <!-- TODO 添加hover dropdown效果 -->
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown"
                       aria-haspopup="true" aria-expanded="false" data-hover="dropdown">
                        <i class="fa fa-list"></i>分类
                    </a>
                    <ul class="dropdown-menu">
                        
                        
                        <li>
                            <a href="/categories/py-study/">py_study</a>
                        </li>
                        
                        <li>
                            <a href="/categories/nlp/">nlp</a>
                        </li>
                        
                        <li>
                            <a href="/categories/Graduation-work/">Graduation work</a>
                        </li>
                        
                        <li>
                            <a href="/categories/work/">work</a>
                        </li>
                        
                        <li>
                            <a href="/categories/hexo/">hexo</a>
                        </li>
                        
                        <li>
                            <a href="/categories/hexo%E5%AE%8C%E5%96%84/">-hexo完善</a>
                        </li>
                        
                        
                        <li>
                            <a href="/categories">...</a>
                        </li>
                        
                        
                    </ul>
                </li>
                
                
                
                
                
                <li class="dropdown">
                    <!-- TODO 添加hover dropdown效果 -->
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown"
                       aria-haspopup="true" aria-expanded="false" data-hover="dropdown">
                        <i class="fa fa-tags"></i>标签
                    </a>
                    <ul class="dropdown-menu">
                        
                        
                        <li>
                            <a href="/tags/py-study/">py_study</a>
                        </li>
                        
                        <li>
                            <a href="/tags/nlp/">nlp</a>
                        </li>
                        
                        <li>
                            <a href="/tags/Graduation-work/">Graduation work</a>
                        </li>
                        
                        <li>
                            <a href="/tags/work/">work</a>
                        </li>
                        
                        <li>
                            <a href="/tags/hexo/">hexo</a>
                        </li>
                        
                        <li>
                            <a href="/tags/%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA/">-个人博客搭建</a>
                        </li>
                        
                        
                        <li>
                            <a href="/tags">...</a>
                        </li>
                        
                        
                    </ul>
                </li>
                
                
                
                
                <li class="">
                    <a href="/about">
                        <i class="fa fa-user"></i>关于我
                    </a>
                </li>
                
                
            </ul>
        </div>
    </div>
</header>
    <div id="gal-body">
        <div class="container">
            <div class="row">
                <div class="col-md-8 gal-right" id="mainstay">
                    
<article class="article well article-body" id="article">
    <div class="breadcrumb">
        <i class="fa fa-home"></i>
        <a href="https://esyyes.github.io">ESY</a>
        >
        <span>python_随机森林参数设置</span>
    </div>
    <!-- 大型设备详细文章 -->
    <div class="hidden-xs">
        <div class="title-article">
            <h1>
                <a href="/2020/03/22/python%20work/python-%E9%9A%8F%E6%9C%BA%E6%A3%AE%E6%9E%97%E5%8F%82%E6%95%B0%E8%AE%BE%E7%BD%AE/">python_随机森林参数设置</a>
            </h1>
        </div>
        <div class="tag-article">
            
            <span class="label label-gal">
                <i class="fa fa-calendar"></i> 2020-03-22
            </span>
            
        </div>
    </div>
    <!-- 小型设备详细文章 -->
    <div class="visible-xs">
        <center>
            <div class="title-article">
                <h4>
                    <a href="/2020/03/22/python%20work/python-%E9%9A%8F%E6%9C%BA%E6%A3%AE%E6%9E%97%E5%8F%82%E6%95%B0%E8%AE%BE%E7%BD%AE/">python_随机森林参数设置</a>
                </h4>
            </div>
            <p>
                <i class="fa fa-calendar"></i> 2020-03-22
            </p>
            <p>
                
                
            </p>
        </center>
    </div>
    <div class="content-article">
        <p><img src="http://i0.hdslb.com/bfs/archive/f252847e6071d16bf5f9b423d759b2c8a0c6eb3d.jpg"></p>
<p><img src="http://i2.hdslb.com/bfs/archive/2c891439af62694315487dc640d3beb6d5709593.jpg"></p>
<h1 id="随机森林参数设置"><a href="#随机森林参数设置" class="headerlink" title="随机森林参数设置"></a>随机森林参数设置</h1><h2 id="随机森林介绍"><a href="#随机森林介绍" class="headerlink" title="随机森林介绍"></a>随机森林介绍</h2><h3 id="随机森林定义"><a href="#随机森林定义" class="headerlink" title="随机森林定义"></a>随机森林定义</h3><p>在<a target="_blank" rel="noopener" href="https://baike.baidu.com/item/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0">机器学习</a>中，**随机森林是一个包含多个决策树的<a target="_blank" rel="noopener" href="https://baike.baidu.com/item/%E5%88%86%E7%B1%BB%E5%99%A8">分类器</a>**， 并且其输出的类别是由个别树输出的类别的众数而定。 Leo Breiman和Adele Cutler发展出推论出随机森林的算法。 而 “Random Forests” 是他们的<a target="_blank" rel="noopener" href="https://baike.baidu.com/item/%E5%95%86%E6%A0%87">商标</a>。 这个术语是1995年由贝尔实验室的Tin Kam Ho所提出的随机决策森林（random decision forests）而来的。这个方法则是结合 Breimans 的 “Bootstrap aggregating” 想法和 Ho 的”random subspace method”以建造<a target="_blank" rel="noopener" href="https://baike.baidu.com/item/%E5%86%B3%E7%AD%96%E6%A0%91">决策树</a>的集合。</p>
<h3 id="随机森林参数定义"><a href="#随机森林参数定义" class="headerlink" title="随机森林参数定义"></a>随机森林参数定义</h3><p><a target="_blank" rel="noopener" href="https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html">https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html</a></p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Longtermevolution/article/details/100919721">https://blog.csdn.net/Longtermevolution/article/details/100919721</a></p>
<p><strong>n_estimators：</strong></p>
<p>森林中数的个数。<br>这个属性是典型的<strong>模型表现与模型效率成反比的影响因子</strong>，即便如此，你还是应该尽可能提高这个数字，以让你的模型更准确更稳定。</p>
<p><strong>criterion ：</strong></p>
<p>度量分裂的标准。可选值：“mse”，均方差（mean squared error）；“mae”，平均绝对值误差（mean absolute error） 支持的标准是基尼杂质的“<strong>gini（基尼）</strong>”和<strong>信息增益的“entropy（熵</strong>）”。注意：此参数是特定于树的。<strong>默认是基尼</strong></p>
<p><strong>max_features</strong> ：</p>
<p>寻找<strong>最佳分裂点时考虑的特征数目</strong>。可选值，int（具体的数目），float（数目的百分比），string（“auto”， “sqrt”，“log2”）.<br>这一属性是对单个树来设置的，通常来讲，<strong>这个值越大单棵树可以考虑的属性越多，则模型的表现就越好</strong>。但是这也不是肯定的，不过有一点是肯定的，增加这个值会导致算法运行速度变慢，所以需要我们考虑去达到一个平衡。</p>
<p>**max_depth :**integer或者None。树的最大深度，如果None，节点扩展直到所有叶子是纯的或者所有叶子节点包含的样例数小于min_samples_split </p>
<p><strong>min_samples_split</strong> ： 分裂内部节点需要的最少样例数。int(具体数目),float(数目的百分比) </p>
<p><strong>min_samples_leaf</strong> ：叶子节点上应有的最少样例数。int(具体数目),float(数目的百分比)。<br>更少的节点数使得模型更容易遭受noise data的影响，我通常设置这个值大于50，但是你需要寻找最适合你的数值。 </p>
<p><strong>min_weight_fraction_leaf ：</strong> </p>
<p><strong>max_leaf_nodes ：</strong>以”最优优先方式”(best-first fashion),最优节点定义为:纯度的相对减少.如果None则不限制叶子节点个数;[float]<br><strong>min_impurity_split :</strong> 树增长提前结束的阈值.对于当前节点,大于这个阈值将分裂,否则就看做叶子节点; [float]<br><strong>min_impurity_decrease ：</strong>一个阈值,表示一个节点分裂的条件是:如果这次分裂纯度的减少大于等于这这个值.<br><strong>bootstrap</strong> ：构建数是不是采用有放回样本的方式(bootstrap samples); [True/False]<br><strong>oob_score ：</strong>交叉验证相关的属性。<br><strong>n_jobs ：</strong>设定fit和predict阶段并列执行的任务个数,<strong>如果设置为-1表示并行执行的任务数等于计算级核数</strong>; [integer, optional (default=1)]<br><strong>random_state</strong> ：如果是<strong>int数值表示它就是随机数产生器的种子</strong>.如果指定RandomState实例,它就是随机产生器的种子.如果是None,随机数产生器是np.random所用的RandomState实例; [int, RandomState instance or None, optional (default=None)]<br><strong>verbose ：</strong>控制<strong>构建数过程的冗长度</strong>; [int, optional (default=0)]<br><strong>warm_start ：</strong>当设置为True,重新使用之前的结构去拟合样例并且加入更多的估计器(estimators,在这里就是随机树)到组合器中; [True/False]<br><strong>class_weight: “</strong>banlanced”模式是根据y标签值自动调整权值与输入数据的类频率成反比,计算公式是:n_samples / (n_classes np.bincount(y)).“balanced_subsample”模式的与”balanced模式相同,只<strong>不过在每一次树增长过程中权值的计算是根据有放回样本的</strong>.</p>
<h2 id="模型调参利器-gridSearchCV（网格搜索）"><a href="#模型调参利器-gridSearchCV（网格搜索）" class="headerlink" title="模型调参利器 gridSearchCV（网格搜索）"></a>模型调参利器 gridSearchCV（网格搜索）</h2><p>GridSearchCV，它存在的意义就是<strong>自动调参</strong>，只要把参数输进去，就能给出<strong>最优化的结果和参数</strong>。但是这个方法适合于<strong>小数据集</strong>，一旦数据的量级上去了，很难得出结果。这个时候就是需要动脑筋了。数据量比较大的时候可以使用一个快速调优的方法——坐标下降。它其实是一种<strong>贪心算法</strong>：<strong>拿当前对模型影响最大的参数调优，直到最优化；再拿下一个影响最大的参数调优</strong>，如此下去，直到所有的参数调整完毕。这个方法的缺点就是可能会调到局部最优而不是全局最优，但是省时间省力，巨大的优势面前，还是试一试吧，后续可以再拿bagging再优化。</p>
<h2 id="2-参数说明"><a href="#2-参数说明" class="headerlink" title="2.参数说明"></a>2.参数说明</h2><p><em>class sklearn.model_selection.<strong>GridSearchCV</strong>(estimator, param_grid, scoring=None, fit_params=None, n_jobs=1, iid=True, refit=True, cv=None, verbose=0, pre_dispatch=‘2\</em>n_jobs’, error_score=’raise’, return_train_score=’warn’)*</p>
<p><strong>（1）</strong>    <strong><em>estimator\</em></strong></p>
<p>选择使用的分类器，并且传入除需要确定最佳的参数之外的其他参数。每一个分类器都需要一个scoring参数，或者score方法：estimator=RandomForestClassifier(min_samples_split=100,min_samples_leaf=20,max_depth=8,max_features=’sqrt’,random_state=10),</p>
<p><strong>（2）</strong>    <strong><em>param_grid\</em></strong></p>
<p>需要最优化的参数的取值，值为字典或者列表，例如：param_grid =param_test1，param_test1 = {‘n_estimators’:range(10,71,10)}。</p>
<p><strong>（3）</strong>    <strong><em>scoring=None\</em></strong></p>
<p>模型评价标准，默认None,这时需要使用score函数；或者如scoring=’roc_auc’，根据所选模型不同，评价准则不同。字符串（函数名），或是可调用对象，需要其函数签名形如：scorer(estimator, X, y)；如果是None，则使用estimator的误差估计函数。具体值的选取看本篇第三节内容。</p>
<p><strong>（4）</strong>    <strong><em>fit_params=None\</em></strong></p>
<p><strong>（5）</strong>    <strong><em>n_jobs=1\</em></strong></p>
<p>n_jobs: 并行数，int：个数,-1：跟CPU核数一致, 1:默认值</p>
<p><strong>（6）</strong>    <strong><em>iid=True\</em></strong></p>
<p><strong>iid</strong>:默认True,为True时，默认为各个样本fold概率分布一致，误差估计为所有样本之和，而非各个fold的平均。</p>
<p><strong>（7）</strong>    <strong><em>refit=True\</em></strong></p>
<p>默认为True,程序将会以交叉验证训练集得到的最佳参数，重新对所有可用的训练集与开发集进行，作为最终用于性能评估的最佳模型参数。即在搜索参数结束后，用最佳参数结果再次fit一遍全部数据集。</p>
<p><strong>（8）</strong>    <strong><em>cv=None\</em></strong></p>
<p>交叉验证参数，默认None，使用三折交叉验证。指定fold数量，默认为3，也可以是yield训练/测试数据的生成器。</p>
<p><strong>（9）</strong>    <strong><em>verbose=0\</em>, *scoring=None*</strong></p>
<p><strong>verbose</strong>：日志冗长度，int：冗长度，0：不输出训练过程，1：偶尔输出，&gt;1：对每个子模型都输出。</p>
<p><strong>（10）</strong>  <strong><em>pre_dispatch=‘2\</em>n_jobs’*</strong></p>
<p>指定总共分发的并行任务数。当n_jobs大于1时，数据将在每个运行点进行复制，这可能导致OOM，而设置pre_dispatch参数，则可以预先划分总共的job数量，使数据最多被复制pre_dispatch次</p>
<p><strong>（11）</strong>  <strong><em>error_score=’raise’\</em></strong></p>
<p><strong>（12）</strong>  <strong><em>return_train_score=’warn’\</em></strong></p>
<p>如果“False”，cv_results_属性将不包括训练分数</p>
<p>回到sklearn里面的GridSearchCV，GridSearchCV用于系统地遍历多种参数组合，通过交叉验证确定最佳效果参数。</p>
<p><strong>打算使用网格搜索对模型进行调优，然后再进行交叉验证！</strong></p>
<h2 id="步骤实现："><a href="#步骤实现：" class="headerlink" title="步骤实现："></a>步骤实现：</h2><ol>
<li>想让树的数目从10到100</li>
</ol>
<p>生成一个10,100的列表</p>
<p>list(range(10,100))</p>
<ol start="2">
<li>构建循环</li>
<li>实现代码</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 初始化模型</span></span><br><span class="line">rfc = RandomForestClassifier()</span><br><span class="line"></span><br><span class="line">tuned_parameter = [&#123; <span class="string">&#x27;n_estimators&#x27;</span>:<span class="built_in">list</span>(<span class="built_in">range</span>(<span class="number">10</span>, <span class="number">100</span>))&#125;]</span><br><span class="line">对模型进行交叉验证，<span class="comment"># 将训练/测试数据集划分10个互斥子集</span></span><br><span class="line"><span class="comment"># kflod = StratifiedKFold(n_splits=10, shuffle = True,random_state=7)</span></span><br><span class="line"><span class="comment"># 将模型进行网格搜索调优</span></span><br><span class="line">clf = GridSearchCV(estimator=rfc,param_grid=tuned_parameter, cv=kflod, n_jobs=<span class="number">1</span>)</span><br><span class="line">clf.fit(X_train, y_train)</span><br><span class="line"><span class="comment"># 将模型进行网格搜索调优</span></span><br><span class="line">print(<span class="string">&quot;Best: %f using %s&quot;</span> % (clf.best_score_,clf.best_params_))</span><br><span class="line">out：</span><br><span class="line">	Best: <span class="number">0.912500</span> using &#123;<span class="string">&#x27;n_estimators&#x27;</span>: <span class="number">28</span>&#125;</span><br></pre></td></tr></table></figure>

<p>没啥必要循环了，每次都在变，选个最高的模型越复杂越好，就选14的倍数，<strong>28</strong></p>
<h2 id="参考博客"><a href="#参考博客" class="headerlink" title="参考博客"></a><strong>参考博客</strong></h2><p>参考这篇对模型进行优化，还可以用下这个模型</p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_41988628/article/details/83098130">https://blog.csdn.net/weixin_41988628/article/details/83098130</a></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd <span class="comment"># 数据科学计算工具</span></span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np <span class="comment"># 数值计算工具</span></span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt <span class="comment"># 可视化</span></span><br><span class="line"><span class="keyword">import</span> seaborn <span class="keyword">as</span> sns <span class="comment"># matplotlib的高级API</span></span><br><span class="line"><span class="keyword">from</span> sklearn.model_selection <span class="keyword">import</span> StratifiedKFold <span class="comment">#交叉验证</span></span><br><span class="line"><span class="keyword">from</span> sklearn.model_selection <span class="keyword">import</span> GridSearchCV <span class="comment">#网格搜索</span></span><br><span class="line"><span class="keyword">from</span> sklearn.model_selection <span class="keyword">import</span> train_test_split <span class="comment">#将数据集分开成训练集和测试集</span></span><br><span class="line"><span class="keyword">from</span> xgboost <span class="keyword">import</span> XGBClassifier                     <span class="comment">#xgboost</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">pima = pd.read_csv(<span class="string">&quot;pima_indians-diabetes.csv&quot;</span>)</span><br><span class="line">print(pima.head())</span><br><span class="line"></span><br><span class="line">x = pima.iloc[:,<span class="number">0</span>:<span class="number">8</span>]</span><br><span class="line">y = pima.iloc[:,<span class="number">8</span>]</span><br><span class="line"></span><br><span class="line">seed = <span class="number">7</span> <span class="comment">#重现随机生成的训练</span></span><br><span class="line">test_size = <span class="number">0.33</span> <span class="comment">#33%测试，67%训练</span></span><br><span class="line">X_train, X_test, Y_train, Y_test = train_test_split(x, y, test_size=test_size, random_state=seed</span><br><span class="line">model = XGBClassifier()               </span><br><span class="line">learning_rate = [<span class="number">0.0001</span>,<span class="number">0.001</span>,<span class="number">0.01</span>,<span class="number">0.1</span>,<span class="number">0.2</span>,<span class="number">0.3</span>] <span class="comment">#学习率</span></span><br><span class="line">gamma = [<span class="number">1</span>, <span class="number">0.1</span>, <span class="number">0.01</span>, <span class="number">0.001</span>]</span><br><span class="line"></span><br><span class="line">param_grid = <span class="built_in">dict</span>(learning_rate = learning_rate,gamma = gamma)<span class="comment">#转化为字典格式，网络搜索要求</span></span><br><span class="line"></span><br><span class="line">kflod = StratifiedKFold(n_splits=<span class="number">10</span>, shuffle = <span class="literal">True</span>,random_state=<span class="number">7</span>)<span class="comment">#将训练/测试数据集划分10个互斥子集，</span></span><br><span class="line"></span><br><span class="line">grid_search = GridSearchCV(model,param_grid,scoring = <span class="string">&#x27;neg_log_loss&#x27;</span>,n_jobs = <span class="number">-1</span>,cv = kflod)</span><br><span class="line"><span class="comment">#scoring指定损失函数类型，n_jobs指定全部cpu跑，cv指定交叉验证</span></span><br><span class="line">grid_result = grid_search.fit(X_train, Y_train) <span class="comment">#运行网格搜索</span></span><br><span class="line">print(<span class="string">&quot;Best: %f using %s&quot;</span> % (grid_result.best_score_,grid_search.best_params_))</span><br><span class="line"><span class="comment">#grid_scores_：给出不同参数情况下的评价结果。best_params_：描述了已取得最佳结果的参数的组合</span></span><br><span class="line"><span class="comment">#best_score_：成员提供优化过程期间观察到的最好的评分</span></span><br><span class="line"><span class="comment">#具有键作为列标题和值作为列的dict，可以导入到DataFrame中。</span></span><br><span class="line"><span class="comment">#注意，“params”键用于存储所有参数候选项的参数设置列表。</span></span><br><span class="line">means = grid_result.cv_results_[<span class="string">&#x27;mean_test_score&#x27;</span>]</span><br><span class="line">params = grid_result.cv_results_[<span class="string">&#x27;params&#x27;</span>]</span><br><span class="line"><span class="keyword">for</span> mean,param <span class="keyword">in</span> <span class="built_in">zip</span>(means,params):</span><br><span class="line">    print(<span class="string">&quot;%f  with:   %r&quot;</span> % (mean,param))</span><br></pre></td></tr></table></figure>

<h1 id="忽略警告"><a href="#忽略警告" class="headerlink" title="忽略警告"></a>忽略警告</h1><p>每次都会出现一大堆的红字，看到就不舒服，以后将这个作为第一个</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> warnings</span><br><span class="line">warnings.filterwarnings(<span class="string">&quot;ignore&quot;</span>)</span><br></pre></td></tr></table></figure>

<p>在PyCharm中提示shadows name ‘xxxx’ from outer scope，当你在外部有个相同名称的变量在方法内部被重新指定了新的值，也就是说你在外部的相同名称的变量压根就没有任何作用。所以PyCharm就回提示这个信息。这个时候就需要我们去调整具体代码了。</p>
<p>意思就是在pycharm中，函数中的变量最好跟外部的变量不一样，要不然就会警告</p>

    </div>
</article>


                </div>
                <aside class="col-md-4 gal-left" id="sidebar">
    <!-- 此为sidebar的搜索框, 非搜索结果页面 -->
<aside id="sidebar-search">
    <div class="search hidden-xs" data-aos="fade-up" data-aos-duration="2000">
        <form class="form-inline clearfix" id="search-form" method="get"
              action="/search/index.html">
            <input type="text" name="s" class="form-control" id="searchInput" placeholder="搜索文章~" autocomplete="off">
            <button class="btn btn-danger btn-gal" type="submit">
                <i class="fa fa-search"></i>
            </button>
        </form>
    </div>
</aside>
    <aside id="sidebar-author">
    <div class="panel panel-gal" data-aos="flip-right" data-aos-duration="3000">
        <div class="panel-heading" style="text-align: center">
            <i class="fa fa-quote-left"></i>
            esy
            <i class="fa fa-quote-right"></i>
        </div>
        <div class="author-panel text-center">
            <img src="/imgs/avatar.jpg" width="140" height="140"
                 alt="个人头像" class="author-image">
            <p class="author-description"><p>esy</p>
</p>
        </div>
    </div>
</aside>
    
    <aside id="sidebar-recent_comments">
    <div class="panel panel-gal recent hidden-xs" data-aos="fade-up" data-aos-duration="2000">
        <div class="panel-heading">
            <i class="fa fa-comments"></i>
            最新评论
            <i class="fa fa-times-circle panel-remove"></i>
            <i class="fa fa-chevron-circle-up panel-toggle"></i>
        </div>
        <ul class="list-group list-group-flush"></ul>
    </div>
</aside>
    
    <!-- 要配置好leancloud才能开启此小工具 -->
    
    
    <aside id="sidebar-recent_posts">
    <div class="panel panel-gal recent hidden-xs" data-aos="fade-up" data-aos-duration="2000">
        <div class="panel-heading">
            <i class="fa fa-refresh"></i>
            近期文章
            <i class="fa fa-times-circle panel-remove"></i>
            <i class="fa fa-chevron-circle-up panel-toggle"></i>
        </div>
        <ul class="list-group list-group-flush">
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/11/05/python%20work/%E6%9C%80%E5%B0%8F%E4%BA%8C%E4%B9%98%E6%B3%95/">最小二乘法</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/11/05/python%20work/%E7%BB%9F%E8%AE%A1%E5%AD%A6%E4%B9%A0-%E7%AC%AC%E4%B8%80%E7%AB%A0/">统计学习--第一章</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/11/04/python%20work/hello-world/">Hello World</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/11/03/python%20work/%E5%88%86%E7%B1%BB%E6%A8%A1%E5%9E%8B%E7%9A%84%E8%AF%84%E4%BC%B0%E6%8C%87%E6%A0%87/">分类模型的评估指标</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/10/21/python%20work/10-21-%E7%88%AC%E8%99%AB%E5%9F%BA%E7%A1%80/">10-21 爬虫基础</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/09/25/python%20work/CRF%E7%9A%84%E6%95%B4%E4%BD%93%E6%B5%81%E7%A8%8B%E7%BB%93%E6%9E%9C/">CRF的整体流程结果</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/09/25/python%20work/nlp-crf%E6%A8%A1%E5%9E%8B/">nlp_crf模型</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/09/25/python%20work/%E6%95%B0%E5%AD%A6%E5%BB%BA%E6%A8%A1%E9%97%AE%E9%A2%983/">数学建模问题3</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/09/25/python%20work/%E6%95%B0%E5%AD%A6%E5%BB%BA%E6%A8%A1%E9%97%AE%E9%A2%982/">数学建模问题2</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/09/25/python%20work/%E6%95%B0%E5%AD%A6%E5%BB%BA%E6%A8%A1%E9%97%AE%E9%A2%981/">数学建模问题1</a>
                </span>
            </li>
            
        </ul>
    </div>
</aside>
    
    
    <aside id="sidebar-rand_posts">
    <div class="panel panel-gal recent hidden-xs" data-aos="fade-up" data-aos-duration="2000">
        <div class="panel-heading">
            <i class="fa fa-refresh"></i>
            随机文章
            <i class="fa fa-times-circle panel-remove"></i>
            <i class="fa fa-chevron-circle-up panel-toggle"></i>
        </div>
        <ul class="list-group list-group-flush">
            
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/10/21/python%20work/10-21-%E7%88%AC%E8%99%AB%E5%9F%BA%E7%A1%80/">10-21 爬虫基础</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/09/03/python%20work/DATA%E6%95%B0%E6%8D%AE%E7%BB%98%E5%88%B6/">DATA数据绘制</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/05/05/python%20work/python-opencv%E5%9F%BA%E7%A1%80%E5%85%A5%E9%97%A8/">python-opencv基础入门</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/05/26/python%20work/python-%E5%85%A5%E9%97%A8%E5%A4%8D%E4%B9%A0%E4%B9%8B%E6%A8%A1%E5%9D%97%E5%92%8C%E5%8C%85/">python-入门复习之模块和包</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/02/07/python%20work/python-%E8%8E%B7%E5%8F%96MIT%E6%95%B0%E6%8D%AE/">python__获取MIT数据</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/03/22/python%20work/python-%E9%9A%8F%E6%9C%BA%E6%A3%AE%E6%9E%97%E5%8F%82%E6%95%B0%E8%AE%BE%E7%BD%AE/">python_随机森林参数设置</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2019/11/20/python%20work/python%E5%9F%BA%E7%A1%80--%E6%8E%A8%E5%AF%BC%E5%BC%8F/">python1</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/08/08/python%20work/q-a%E6%95%B0%E6%8D%AE%E8%A1%A5%E5%85%85/">q&amp;a数据补充</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/07/16/python%20work/q-a%E6%A3%80%E7%B4%A2%E5%BC%8F%E6%9C%BA%E5%99%A8%E4%BA%BA%E7%9A%84%E6%B5%81%E7%A8%8B/">q@a检索式机器人的流程</a>
                </span>
            </li>
            
            <li class="list-group-item">
                <span class="post-title">
                    <a href="/2020/07/08/python%20work/%E7%8A%B6%E6%80%81%E4%B8%BA3%E6%97%B6%E7%9A%84%E8%AE%BE%E5%AE%9A/">状态为3时的设定</a>
                </span>
            </li>
            
        </ul>
    </div>
</aside>
    
    
    <aside id="gal-sets">
        <div class="panel panel-gal hidden-xs" data-aos="fade-up" data-aos-duration="2000">
            <ul class="nav nav-pills pills-gal">

                
                <li>
                    <a href="/2020/03/22/python%20work/python-%E9%9A%8F%E6%9C%BA%E6%A3%AE%E6%9E%97%E5%8F%82%E6%95%B0%E8%AE%BE%E7%BD%AE/index.html#sidebar-tags" data-toggle="tab" id="tags-tab">热门标签</a>
                </li>
                
                
                <li>
                    <a href="/2020/03/22/python%20work/python-%E9%9A%8F%E6%9C%BA%E6%A3%AE%E6%9E%97%E5%8F%82%E6%95%B0%E8%AE%BE%E7%BD%AE/index.html#sidebar-friend-links" data-toggle="tab" id="friend-links-tab">友情链接</a>
                </li>
                
                
                <li>
                    <a href="/2020/03/22/python%20work/python-%E9%9A%8F%E6%9C%BA%E6%A3%AE%E6%9E%97%E5%8F%82%E6%95%B0%E8%AE%BE%E7%BD%AE/index.html#sidebar-links" data-toggle="tab" id="links-tab">个人链接</a>
                </li>
                
            </ul>
            <div class="tab-content">
                
                <div class="cloud-tags tab-pane nav bs-sidenav fade" id="sidebar-tags">
    
    <a href="/tags/py-study/" style="font-size: 18.606034336855px;" class="tag-cloud-link">py_study</a>
    
    <a href="/tags/nlp/" style="font-size: 17.135781984351336px;" class="tag-cloud-link">nlp</a>
    
    <a href="/tags/Graduation-work/" style="font-size: 15.703900126720825px;" class="tag-cloud-link">Graduation work</a>
    
    <a href="/tags/work/" style="font-size: 17.472610484777405px;" class="tag-cloud-link">work</a>
    
    <a href="/tags/hexo/" style="font-size: 13.183967595130348px;" class="tag-cloud-link">hexo</a>
    
    <a href="/tags/%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA/" style="font-size: 9.611994066541573px;" class="tag-cloud-link">-个人博客搭建</a>
    
    <a href="/tags/malab-%E6%AF%95%E4%B8%9A/" style="font-size: 16.511355840071406px;" class="tag-cloud-link">-malab -毕业</a>
    
    <a href="/tags/python-%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD/" style="font-size: 15.225095826130529px;" class="tag-cloud-link">-python -人工智能</a>
    
    <a href="/tags/python/" style="font-size: 15.194639750589896px;" class="tag-cloud-link">python</a>
    
    <a href="/tags/python/" style="font-size: 14.568430271900457px;" class="tag-cloud-link">-python</a>
    
    <a href="/tags/mathematical-modeling/" style="font-size: 18.14118595244594px;" class="tag-cloud-link">mathematical modeling</a>
    
    <a href="/tags/statistical-learning/" style="font-size: 18.070392385196005px;" class="tag-cloud-link">statistical learning</a>
    
</div>
                
                
                <div class="friend-links tab-pane nav bs-sidenav fade" id="sidebar-friend-links">
    
    <li>
        <a href="http://kdays.net/days/" target="_blank">KDays Forum</a>
    </li>
    
    <li>
        <a href="http://www.gal123.com/" target="_blank">绅士导航♂</a>
    </li>
    
    <li>
        <a href="http://www.moe123.com/" target="_blank">萌导航</a>
    </li>
    
</div>
                
                
                <div class="links tab-pane nav bs-sidenav fade" id="sidebar-links">
    
    <li>
        <a href="https://github.com/ZEROKISEKI/" target="_blank">Github</a>
    </li>
    
    <li>
        <a href="https://coding.net/u/SORA1" target="_blank">Coding</a>
    </li>
    
    <li>
        <a href="https://www.zhihu.com/people/aonosora/activities" target="_blank">知乎</a>
    </li>
    
</div>
                
            </div>
        </div>
    </aside>
    
</aside>
            </div>
        </div>
    </div>
    <footer id="gal-footer">
    <div class="container">
        Copyright © 2018 esy Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>.&nbsp;Theme by <a href="https://github.com/ZEROKISEKI" target="_blank">AONOSORA</a>
    </div>
</footer>

<!-- 回到顶端 -->
<div id="gal-gotop">
    <i class="fa fa-angle-up"></i>
</div>
</body>

<script src="/js/activate-power-mode.js"></script>

<script>

    // 配置highslide
	hs.graphicsDir = '/js/highslide/graphics/'
    hs.outlineType = "rounded-white";
    hs.dimmingOpacity = 0.8;
    hs.outlineWhileAnimating = true;
    hs.showCredits = false;
    hs.captionEval = "this.thumb.alt";
    hs.numberPosition = "caption";
    hs.align = "center";
    hs.transitions = ["expand", "crossfade"];
    hs.lang.number = '共%2张图, 当前是第%1张';
    hs.addSlideshow({
      interval: 5000,
      repeat: true,
      useControls: true,
      fixedControls: "fit",
      overlayOptions: {
        opacity: 0.75,
        position: "bottom center",
        hideOnMouseOut: true
      }
    })

    // 初始化aos
    AOS.init({
      duration: 1000,
      delay: 0,
      easing: 'ease-out-back'
    });

</script>
<script>
	POWERMODE.colorful = 'true';    // make power mode colorful
	POWERMODE.shake = 'true';       // turn off shake
	// TODO 这里根据具体情况修改
	document.body.addEventListener('input', POWERMODE);
</script>
<script>
    window.slideConfig = {
      prefix: '/imgs/slide/background',
      ext: 'jpg',
      maxCount: '6'
    }
</script>

<script src="/js/hs.js"></script>
<script src="/js/blog.js"></script>



<script src="/js/oni.js"></script>




</html>